<template>
    <div>
        <el-page-header @back="gotoRoute('/')" content="二级指标设置">
        </el-page-header>
        <el-divider></el-divider>
        <el-button-group>
            <el-button size="mini" type="primary" @click="handEdit">新增</el-button>
        </el-button-group>
        <el-divider></el-divider>
        <el-tag type="warning">请按照【175号文件】规定顺序排序</el-tag>
        <el-table :data="tableData.rows" border class="w-80%">
            <el-table-column label="排序[请在一级指标内,按照实际排序]" width="220">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button size="mini" type="info" @click="deltaSort(scope.row.id, scope.row.sort, -1)"><i
                                class="fa fa-arrow-up"></i> </el-button>
                        <el-button size="mini" type="warning" @click="deltaSort(scope.row.id, scope.row.sort, 1)"><i
                                class="fa fa-arrow-down" aria-hidden="true"></i></el-button>
                    </el-button-group>

                </template>
            </el-table-column>
            <el-table-column label="顺序" width="50">
                <template slot-scope="scope">
                    <el-tag type="info">{{ scope.row.sort + 1 }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="名称" width="280">
            </el-table-column>
            <el-table-column prop="top_name" label="一级指标" width="280">
            </el-table-column>
            <el-table-column prop="remark" label="备注" width="220">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="handEdit(scope.row)" type="success" size="small">编辑</el-button>
                    <el-button @click="handRemove(scope.row)" type="danger" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @current-change="handPage" layout="prev, pager, next" :total="tableData.count"
            :page-size="tableData.offset">
        </el-pagination>
    </div>
</template>

<script>
import { FormDialog } from '@formily/element'
import { InputForm } from './InputForm';
import { RuSecondRuleApi } from '../../api/ru-second-rule';
import { sharedConfig } from '../shared/BaseCurd';
import { onFieldValueChange } from '@formily/core';
const api = new RuSecondRuleApi();
const entity = {
    id: undefined,
    name: "",
    top_rule: "",
    eva_target: "单位",
    rule_type: "",
    is_required: true,
    options: ``,
    values: "",
    min_select: 1,
    max_select: 1,
    remark: "",
}



export default {
    async mounted() {
        this.loadPager({ page: 1 });
    },
    methods: {
        ...sharedConfig.methods,
        async loadPager({ page }) {
            const data = await api.findPager({ page, offset: 50, order: [['top_id'], ['sort']] });
            this.tableData = { ...data.data };
        },
        handEdit(row) {
            FormDialog('编辑二级指标', InputForm)
                .forOpen((payload, next) => {
                    const raw = (row instanceof PointerEvent) ? entity : row;
                    next({
                        initialValues: { ...raw },
                        effects() {
                            onFieldValueChange("top_info", (field, form) => {
                                const values = field.value.split('^');
                                if (values) {
                                    form.query("top_id").take().value = values[0];
                                    form.query("top_name").take().value = values[1];
                                }

                            })
                        }
                    })
                })
                .forConfirm((payload, next) => {
                    next(payload)
                })
                .forCancel((payload, next) => {
                    next(payload)
                })
                .open()
                .then(async (values) => {
                    Reflect.deleteProperty(values, "top_info");
                    await this._save({ api, values })
                    await this.loadPager({ page: 1 });
                })
                .catch(console.error)
        },
        handRemove(row) {
            this.$confirm("确定要删除?", "系统提示").then(async () => {
                await this._remove({ api, where: { id: row.id } });
                await this.loadPager(1);
            }).catch(console.error);
        },
        async deltaSort(id, sort, delta) {
            await this.sort(api, id, sort, delta)
        }
    },

    data() {
        return {
            tableData: {
                count: 0,
                offset: 10,
                page: 1,
                rows: [],
            }
        }
    }
}
</script>